// ignore_for_file: file_names, prefer_final_fields, always_specify_types

import 'dart:io';

import 'package:flutter/material.dart';
// ignore_for_file: prefer_const_constructors
import '../../entity/User.dart';
import '../dialog/ChangePWDialog.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image_crop/image_crop.dart';

import 'Home.dart';
import 'Search.dart';
import 'Message.dart';
import 'CropImageRoute.dart';

class Tabs extends StatefulWidget {
  const Tabs({Key? key}) : super(key: key);
  static String image_head = User.getUserHeadImageUrl();

  @override
  _TabsState createState() => _TabsState(image_head);
}

class _TabsState extends State<Tabs> {
  int _currentIndex = 0;
  String _head_image;

  List _pageList = [HomePage(), SearchPage(), MessagePage()];

  _TabsState(this._head_image);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('主动脉夹层辅助检测系统'),
        leading: Builder(builder: (BuildContext context) {
          return IconButton(
            icon: CircleAvatar(
              backgroundImage: NetworkImage(User.getUserHeadImageUrl()),
            ),
            onPressed: () {
              Scaffold.of(context).openDrawer();
            },
            tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
          );
        }),
      ),
      body: this._pageList[this._currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (int index) {
          // ignore: avoid_print
          print(index);
          setState(() {
            _currentIndex = index;
          });
        },
        iconSize: 30.0,
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
          BottomNavigationBarItem(icon: Icon(Icons.search), label: '搜索'),
          BottomNavigationBarItem(icon: Icon(Icons.message), label: '消息')
        ],
      ),
      drawer: Drawer(
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Expanded(
                    child: UserAccountsDrawerHeader(
                  accountName: Text(User.name),
                  accountEmail: Text('工号：${User.job_num}'),
                  currentAccountPicture: GestureDetector(
                    onTap: () {
                      Navigator.of(context).push(MaterialPageRoute(
                          builder: (context) => CropPage() //此处监听按钮跳转，未传值
                          ));
                      setState(() {
                        _head_image = User.getUserHeadImageUrl();
                      });
                    },
                    child: CircleAvatar(
                      backgroundImage: NetworkImage(User.getUserHeadImageUrl()),
                    ),
                  ),
                  decoration: BoxDecoration(
                      image: DecorationImage(
                    image: NetworkImage(
                        'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0380d7d55deb3de00000159950942ae.jpg&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636823390&t=6c5f57af1862f2e5cd6159638d00bcf5'),
                    fit: BoxFit.cover,
                  )),
                  otherAccountsPictures: User.type == 1
                      ? [Image.asset('assets/img/expert.png')]
                      : [],
                ))
              ],
            ),
            ListTile(
              leading: CircleAvatar(
                child: Icon(Icons.local_hospital_outlined),
              ),
              title: Text('所在医院：${User.hospital}'),
            ),
            Divider(),
            ListTile(
              leading: CircleAvatar(
                child: Icon(Icons.phone),
              ),
              title: Text('联系电话：${User.phone_num}'),
            ),
            Divider(),
            ListTile(
              leading: CircleAvatar(
                child: Icon(Icons.settings),
              ),
              title: Text('修改信息'),
              onTap: () {
                Navigator.pushNamed(context, '/changeinfo');
              },
            ),
            Divider(),
            ElevatedButton(
                onPressed: () {
                  showDialog(
                      context: context,
                      builder: (context) {
                        return ChangePWDialog();
                      });
                },
                child: Text('修改密码')),
            Divider(),
            ElevatedButton(
                onPressed: () {
                  Navigator.pushNamed(context, '/login');
                },
                child: Text('退出登录')),
          ],
        ),
      ),
    );
  }

  setInfo() {
    setState(() {
      _head_image = User.head_image;
    });
  }

  // _expertOrNot() {
  //   if (User.type == 1) {
  //     return Widget();
  //   } else {
  //     return [];
  //   }
  // }
}
